package com.daka.service.impl;

import com.daka.dao.UActiivtyDao;
import com.daka.dao.UActivityApplyDao;
import com.daka.dao.UActivitySignDao;
import com.daka.dao1.ActivityApplyDao;
import com.daka.dao1.ActivityDao;
import com.daka.dao1.ActivitySignDao;
import com.daka.entity.*;
import com.daka.service.ActivityService;
import com.daka.tools.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Copyright: Copyright (c)2016
 * Company: 志愿者打卡器(www.dakaqi.cn)
 * 类名称: ActivityServiceImpl <br>
 * 类描述: <br>
 *
 * @author: ChunYang.Zhang
 * @since: 2016/11/22 14:02
 * @version: 1.0.0
 */
@Component(value = "activityService")
public class ActivityServiceImpl implements ActivityService
{
    @Autowired
    ActivityDao activityDao;
    @Autowired
    UActiivtyDao uactiivtyDao;
    @Autowired
    ActivityApplyDao activityApplyDao;
    @Autowired
    UActivityApplyDao uActivityApplyDao;
    @Autowired
    ActivitySignDao activitySignDao;
    @Autowired
    UActivitySignDao uActivitySignDao;


    public void importActivity()
    {
        importActivity2Unicron();
    }

    private void importActivity2Unicron()
    {
        int SIZE = 1000;
        int totalPage = this.activityDao.findAllCount(0)/1000+1;
        for(int i=1;i<=totalPage;i++)
        {
            if(i == 2)
                break;
            List<Activity> acts = this.activityDao.findByPage(0,i-1,SIZE);
            for(Activity act:acts)
            {
                //导入活动
                UActiivty uActiivty = Activity.buildUActivity(act, orgId2GroupId(act.getOrganization()), memberId2VolunteerId(act.getCreateuser()));
                this.uactiivtyDao.insertAndGetId(uActiivty);
                int actID = act.getId();
                int organization = act.getOrganization();
                Long U_ACTIVITY_ID = uActiivty.getId();

                //导入活动报名
                importActivityApply2Unicron(actID,U_ACTIVITY_ID);
                //导入活动公益记录
                importActivitySign2Unicron(actID,organization,U_ACTIVITY_ID);
            }


            //uacts.add(Activity.buildUActivity(act, orgId2GroupId(act.getOrganization()), memberId2VolunteerId(act.getCreateuser())));
            //if(null != uacts && uacts.size()>0)
            //    this.uactiivtyDao.insertBatch(uacts);
        }
    }
    private void importActivityApply2Unicron(int actID,Long U_ACTIVITY_ID)
    {
        try
        {
            List<ActivityApply> applies = activityApplyDao.findByActivity(actID);
            if(null != applies && applies.size()>0)
            {
                List<UActivityApply> uaa = new ArrayList<UActivityApply>();
                for(ActivityApply aa:applies)
                {
                    UActivityApply uActivityApply = new UActivityApply();
                    uActivityApply.setActivityId(U_ACTIVITY_ID);
                    uActivityApply.setVolunteerId(memberId2VolunteerId(aa.getMember()));
                    uActivityApply.setStatus(2);
                    uActivityApply.setRole(aa.getUserType());
                    uActivityApply.setCreateTime(DateUtil.DefaultTimeFormatter.parse(aa.getCreatetime()));
                    uActivityApply.setDelStatus(0);
                    uActivityApply.setRefuseCase("");
                    uaa.add(uActivityApply);
                }

                if(null != uaa && uaa.size()>0)
                {
                    uActivityApplyDao.insertBatch(uaa);
                }
            }
        } catch (ParseException e)
        {
            e.printStackTrace();
        }
    }
    private void importActivitySign2Unicron(int actID,int organization,Long U_ACTIVITY_ID)
    {
        try
        {
            List<ActivitySign> signs = activitySignDao.findByActivity(actID);
            if(null != signs && signs.size()>0)
            {
                List<UActivitySign> uas = new ArrayList<UActivitySign>();
                for(ActivitySign as:signs)
                {
                    UActivitySign uActivitySign = new UActivitySign();
                    uActivitySign.setCreateTime(DateUtil.DefaultTimeFormatter.parse(as.getCreatetime()));
                    uActivitySign.setDelStatus(as.getDelstatus());
                    uActivitySign.setLat(as.getLat());
                    uActivitySign.setLng(as.getLng());
                    uActivitySign.setStatus(0);
                    uActivitySign.setTimes(as.getVolunteertime());
                    uActivitySign.setActivityId(U_ACTIVITY_ID);
                    uActivitySign.setCommnetType(as.getCommenttype());
                    uActivitySign.setHasCommnet(1);
                    uActivitySign.setGroupId(orgId2GroupId(organization));
                    uActivitySign.setOrgs("");
                    uActivitySign.setRecordId(Long.valueOf(""+as.getRecordid()));
                    uActivitySign.setVolunteerId(memberId2VolunteerId(as.getMember()));
                    uas.add(uActivitySign);
                }

                if(null != uas && uas.size()>0)
                {
                    uActivitySignDao.insertBatch(uas);
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public long findByOldId(int oldid)
    {
        return activityDao.findByOldId(oldid);
    }

    private long orgId2GroupId(int orgId)
    {
        long groupId = 108L;

        return groupId;
    }
    private long memberId2VolunteerId(int memberId)
    {
        long volunteerId = 302L;
        return volunteerId;
    }
}
